<?php
include("model/imports.php");
class importsController{
	
	function indexAction(){
		$smarty = $_SESSION['smarty'];
		
		$list_imports_box = $this->getListImportsBox();
		$smarty->assign("list_imports_box",$list_imports_box);
		
		$content = $smarty->fetch("imports/imports.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function getListImportsBox(){
		$smarty 	= $_SESSION['smarty'];
		$import_obj = new imports();
		$importsList	= $import_obj->getImportsList();		
		$smarty->assign("importsList", $importsList);
		$html 		= $smarty->fetch("imports/imports_list.html");
		return $html;
	}
	
	function insertAction(){
		$imports_obj = new imports();
		
		$data 		= array();
		$data['im_date'] 			= $_REQUEST['im_date'];
		$data['im_emp_id'] 			= $_REQUEST['im_emp_id'];
		$data['im_sup_id'] 			= $_REQUEST['im_sup_id'];
		$data['im_pay_id']			= $_REQUEST['im_pay_id'];
		
		$transaction = array();
		for ($i=1;$i<=3;$i++)
		{		
			$transaction[$i]['reci_pro_id'] 	= $_REQUEST["reci_pro_id$i"];
			$transaction[$i]['reci_propla_id'] 	= $_REQUEST["reci_propla_id$i"];
			$transaction[$i]['reci_quantity'] 	= $_REQUEST["reci_quantity$i"];
			$transaction[$i]['reci_price'] 		= $_REQUEST["reci_price$i"];
		}
	
		$result = $imports_obj->insertAImport($data,$transaction);
		if ($result == 1)
			$msg = "insert successfully";
		else if ($result == -1)
			$msg = "duplicated!";
		else
			$msg = "insert failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("imports/imports_insertResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
		
	}
	
	function inactiveAction(){
		$im_id 		= $_REQUEST['args'];
		$imports_obj	=	new imports();
		$result 		= $imports_obj->inactivateAImport($im_id);
		if ($result == 1)
			$msg = "inactivate successfully";		
		else
			$msg = "inactivate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_imports_box = $this->getListImportsBox();
		$smarty->assign("list_imports_box",$list_imports_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("imports/imports.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function activeAction(){
		$im_id 		= $_REQUEST['args'];
		$imports_obj 	= new imports();
		$result 		= $imports_obj->activateAImport($im_id);
		if ($result == 1)
			$msg = "activate successfully";		
		else
			$msg = "activate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_imports_box = $this->getListImportsBox();
		$smarty->assign("list_imports_box",$list_imports_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("imports/imports.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function insertFormAction(){
		$smarty = $_SESSION['smarty'];		
		
		$employeesList = array();
		include("model/employees.php");
		$employees_obj = new employees();
		$employeesList = $employees_obj->getEmployeesList();
		
		$suppliersList = array();
		include("model/suppliers.php");
		$suppliers_obj = new suppliers();
		$suppliersList = $suppliers_obj->getSupplierList();
		
		$paymentsList = array();
		include("model/payments.php");
		$payments_obj = new payments();
		$paymentsList = $payments_obj->getPaymentsList();
		
		$productList = array();
		include("model/products.php");
		$products = new products();
		$productList = $products->getProductList();
		
		$placeList = array();
		include("model/places.php");
		$places = new places();
		$placeList = $places->getPlaceList();
		
		$smarty->assign("employeesList", $employeesList);
		$smarty->assign("suppliersList", $suppliersList);
		$smarty->assign("paymentsList", $paymentsList);
		$smarty->assign("productList", $productList);
		$smarty->assign("placeList", $placeList);
		
		$insertform_imports_box = $smarty->fetch("imports/imports_insert_form.html");
		
		$smarty->assign("insertform_imports_box",$insertform_imports_box);
		
		$content = $smarty->fetch("imports/imports.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function editFormAction(){
		$im_id 			= $_REQUEST['args'];
		$smarty 			= $_SESSION['smarty'];		
		
	$employeesList = array();
		include("model/employees.php");
		$employees_obj = new employees();
		$employeesList = $employees_obj->getEmployeesList();
		
		$suppliersList = array();
		include("model/suppliers.php");
		$suppliers_obj = new suppliers();
		$suppliersList = $suppliers_obj->getSupplierList();
		
		$paymentsList = array();
		include("model/payments.php");
		$payments_obj = new payments();
		$paymentsList = $payments_obj->getPaymentsList();
		
		$smarty->assign("employeesList", $employeesList);
		$smarty->assign("suppliersList", $suppliersList);
		$smarty->assign("paymentsList", $paymentsList);
		
		$imports_obj 		= new imports();
		$current_data 		= array();
		$current_data 		= $imports_obj->getImportsInfo($im_id);
		
		$smarty->assign("current_data", $current_data);
		
		$editform_imports_box = $smarty->fetch("imports/imports_edit_form.html");
		$smarty->assign("editform_imports_box",$editform_imports_box);
		
		$content = $smarty->fetch("imports/imports.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");	
	}
	
	function editAction(){
		$imports_obj 	= new imports();
		$data 			= array();
		$data['im_id'] 				= $_REQUEST['im_id'];
		$data['im_date'] 			= $_REQUEST['im_date'];
		$data['im_emp_id']		 	= $_REQUEST['im_emp_id'];
		$data['im_sup_id']		 	= $_REQUEST['im_sup_id'];
		$data['im_pay_id']		 	= $_REQUEST['im_pay_id'];	
		
		
		$result = $imports_obj->editAImport($data);
		
		if ($result == 1)
			$msg = "edit successfully";
		else
			$msg = "update failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("imports/imports_updateResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function detailAction(){
		$im_id 			= $_REQUEST['args'];
		$smarty 		= $_SESSION['smarty'];	
		
		$import_obj 	= new imports();
		$importDetail	= $import_obj->getImportDetailById($im_id);
		$smarty->assign("importDetail", $importDetail);
		$content = $smarty->fetch("imports/imports_detail.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");	
		
	}
	
	
}
?>